package com.api.workflow.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.workflow.util.YNHttpUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
 * 资产调拨接口  公司间调拨类型
 *
 * */
public class YNFII013Action extends BaseBean implements Action {
    public static void main(String[] args) {

    }
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String workflowid = requestInfo.getWorkflowid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName ="formtable_main_"+formidAbs;
        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();
        //这里是存储了主表全部的数据  可自行修改存储结构
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        baseBean.writeLog("资产调拨接口===========mainTableInfoMap:"+ JSONObject.toJSONString(mainTableInfoMap));

        //明细表数据
        List<Map<String, Object>> detailTableMap = getMaps(requestInfo);
        baseBean.writeLog("资产调拨接口=======detailTableMap:"+ JSONObject.toJSONString(detailTableMap));

        //业务逻辑
        String message = "";
        String dblx = String.valueOf(mainTableInfoMap.get("dblx"));
        if (dblx.equals("ND")) {
            try {


                RecordSet rs = new RecordSet();

                String sqbm = "";
                Map<String, Object> map = new HashMap<>();

                map.put("ZIFSN", mainTableInfoMap.get("lcbh"));

                String sqr = mainTableInfoMap.get("sqr").toString();
                String sql1 = "select * from hrmresource where id = '" + sqr + "'";
                baseBean.writeLog("wyq===资产调拨接口===========sql1" + sql1);

                rs.execute(sql1);
                if (rs.next()) {
                    sqbm = rs.getString("departmentid");
                    map.put("ZUSER", rs.getString("lastname"));
                    baseBean.writeLog("wyq===资产调拨接口=========lastname" + rs.getString("lastname"));
                }

                String sql2 = "select * from hrmdepartment where id = '" + sqbm + "'";
                baseBean.writeLog("wyq===资产调拨接口===========sql2" + sql2);

                rs.execute(sql2);
                if (rs.next()) {
                    map.put("ZSQBM", rs.getString("departmentcode"));
                    map.put("ZBMMS", rs.getString("departmentname"));
                    baseBean.writeLog("wyq===资产调拨接口=========ZSQBM" + rs.getString("departmentcode") + "ZBMMS:" + rs.getString("departmentname"));
                }
                map.put("BUKRS", mainTableInfoMap.get("gsdm"));


                ArrayList<Map> list = new ArrayList<>();


                int count = 1;
                for (Map<String, Object> stringObjectMap : detailTableMap) {
                    Map<String, Object> ITEM = new HashMap<>();
                    ITEM.put("ZDBHID", count);
                    count++;
                    ITEM.put("ZDBLX", "Z01");
                    ITEM.put("ZANLN1", stringObjectMap.get("xzzc"));
                    ITEM.put("ZTXT50Z", stringObjectMap.get("zcmc"));
                    ITEM.put("ZBUKRS1", mainTableInfoMap.get("gsdm"));
                    ITEM.put("ZKOSTL1", stringObjectMap.get("dccbzx"));
                    ITEM.put("H_BLDAT", stringObjectMap.get("pzrq"));
                    ITEM.put("H_BUDAT", stringObjectMap.get("gzrq"));
                    ITEM.put("BZDAT", stringObjectMap.get("zcqxr"));
                    ITEM.put("ZDCJZ", stringObjectMap.get("dczcyz"));
                    ITEM.put("ZDCZJ", stringObjectMap.get("ljzj"));
                    ITEM.put("ZDCJZE", stringObjectMap.get("zcjz"));
                    ITEM.put("ZDCYYF", stringObjectMap.get("syyf"));
                    ITEM.put("ZDCSYF", stringObjectMap.get("sysyyf"));
                    ITEM.put("ZBUKRS2", stringObjectMap.get("drgsdm"));
                    ITEM.put("ZKOSTL2", stringObjectMap.get("drcbzx"));
                    ITEM.put("ZDBGM", stringObjectMap.get("sfwgm"));
                    ITEM.put("ZGMJE", stringObjectMap.get("gmje"));

                    list.add(ITEM);
                }

                map.put("ITEMS", list);

                JSONObject object = new JSONObject(map);

                baseBean.writeLog("wyq===资产调拨接口=========object" + object.toJSONString());
                String post = YNHttpUtils.FII013(object.toJSONString(), YNHttpUtils.token());

                baseBean.writeLog("wyq===资产调拨接口=========post" + post);

               JSONObject jsonObject = JSONObject.parseObject(post);
                baseBean.writeLog("wyq===资产调拨接口=========jsonObject" + jsonObject.toString());


                String STATUS = jsonObject.getString("STATUS");
                String MESSAGE = jsonObject.getString("MESSAGE");

                baseBean.writeLog("wyq===资产调拨接口===========资产调拨接口");

                RecordSet recordSet = new RecordSet();
                //修改主表参数
                String updateMainTableName = "update " + tableName + " set qqcsx = ? , fhjgx = ?  where  requestid = ? ";
                recordSet.executeUpdate(updateMainTableName, object.toString(), post, requestid);

                baseBean.writeLog("wyq===资产调拨接口===========updateMainTableName" + updateMainTableName);


                JSONArray jsonArray = jsonObject.getJSONArray("RETURN");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                    String TYPE = jsonObject1.getString("TYPE");
                    message = MESSAGE + jsonObject1.getString("MESSAGE");

                    if (!"S".equals(TYPE)) {
                        requestInfo.getRequestManager()
                                .setMessageid(requestInfo.getRequestid() + "OA-资产调拨接口");
                        requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:" + message);
                        return Action.FAILURE_AND_CONTINUE;
                    }


                }


            } catch (Exception e) {
                baseBean.writeLog("wyq===资产调拨接口======exc" + e.toString());
                requestInfo.getRequestManager()
                        .setMessageid(requestInfo.getRequestid() + "OA-资产调拨接口");
                requestInfo.getRequestManager().setMessagecontent("OA接口异常：" + e.toString());
                return Action.FAILURE_AND_CONTINUE;
            }
        }

        return Action.SUCCESS;
    }

    /**
     * 提取明细表全部的数据
     * @param requestInfo
     * @return
     */
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);
                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }
}
